<#include "/admin/common/include/html_head.html"> <#include
"/admin/common/include/sysparam.html"> <#include
"/admin/common/include/pagination.html">

<div class="row-fluid">
	<div class="span12">
		<!-- BEGIN EXAMPLE TABLE PORTLET-->
		<div class="portlet box">
			<div class="portlet-title">
				<div class="caption">
					<h5>日志查看</h5>
				</div>
			</div>
			<div class="portlet-body form" id="logFrame">

				<div class="span3" style="margin: auto; height: inherit;">
					<ul id="tree" class="ztree"
						style="margin: auto; height: inherit; overflow: auto;"></ul>
				</div>
				<div class="span9" style="margin: auto; height: inherit;"
					id="logArea" lastreadposition="0" filepath="">
					<div class="alert alert-error " id="errtip" style="display: none;"></div>
					<textarea class="span12" name="logContent" rows="25"
						id="logContent"> </textarea>
				</div>
			</div>
			<!-- END EXAMPLE TABLE PORTLET-->
		</div>
	</div>
</div>

<script>
	var zTree;
	var demoIframe;
	var setting = {

		view : {
			//dblClickExpand : false,
			showLine : false
		//,
		//selectedMulti : false
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "pId",
				rootPId : "",
				file : "file"
			}
		},
		callback : {
			beforeClick : function(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				if (treeNode.isParent) {
					zTree.expandNode(treeNode);
					return false;
				} else {
					demoIframe.attr("src", treeNode.file + ".html");
					return true;
				}
			},
			onClick : function(event, treeId, treeNode) {
				var readLastMinLen = 500;
				// 每次读取的最大长度 
				var readMaxLen = 500;
				// 文件路径 
				var filePath = treeNode.file;
				// 最后读取文件位移 
				var lastReadPosition = 0;
				var readRequest = {};
				readRequest["readLastMinLen"] = 20000;
				readRequest["readMaxLine"] = 20;
				readRequest["filePath"] = treeNode.file;
				//获取当前文件读取的位置
				readRequest["lastReadPosition"] = getCurrentFilePosition(filePath);
	
				var readRequestJson = JSON.stringify(readRequest);
				//alert(readRequestJson);
				ajax.syncJsonRequest({
					param : readRequestJson,
					url : '${contextPath}/readlog.op',
					success : function(data) {
						if (data.resultCode == 0) {
							$('#logContent').append(data.content);
							var ta = document.getElementById('logContent');
							ta.scrollTop = ta.scrollHeight;
							$('#errtip').html('');
							$('#errtip').hide();
							//记录文件读取后的位置
							recordFileStat(filePath, data.lastReadPosition);
						} else {
							/* common.alert({
								content : data.resultMsg
							}); */
							$('#errtip').html(data.resultMsg);
							$('#errtip').show();
							return;
						}
					},
					err : function() {
						alert('Err...');
					}
				});
			}
		}
	};

	function recordFileStat(filePath, lastReadPosition) {
		if (!filePath)
			return;
		console.log('start record lastReadPosition:' + lastReadPosition);
		var currentFilePath = $('#logArea').attr('filePath');
		$('#logArea').attr('lastreadposition', lastReadPosition);
		$('#logArea').attr('filepath', filePath);

	}

	function getCurrentFilePosition(filePath) {
		if (!filePath)
			return;
		var currentFilePath = $('#logArea').attr('filepath');

		if (!currentFilePath || !(filePath == currentFilePath)) {
			$('#logContent').html('');
			$('#logArea').attr('lastreadposition', '0');
			console.log('getCurrentFilePosition 0:0');
			return 0;
		} else {
			console.log('getCurrentFilePosition 1:'
					+ $('#logArea').attr('lastreadposition'));
			return $('#logArea').attr('lastreadposition');
		}
	}

	function initTree() {
		var t = $("#tree");
		var categoryTreeJson;
		ajax.syncJsonRequest({
			url : '${contextPath}/viewLogTree.op?projectId=1',
			success : function(data) {
				categoryTreeJson = data;
			}
		});
		t = $.fn.zTree.init(t, setting, categoryTreeJson);
		demoIframe = $("#testIframe");
		demoIframe.bind("load", loadReady);
		var zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.selectNode(zTree.getNodeByParam("id", 101));
	}

	function loadReady() {
		var bodyH = demoIframe.contents().find("body").get(0).scrollHeight, htmlH = demoIframe
				.contents().find("html").get(0).scrollHeight, maxH = Math.max(
				bodyH, htmlH), minH = Math.min(bodyH, htmlH), h = demoIframe
				.height() >= maxH ? minH : maxH;
		if (h < 530)
			h = 530;
		demoIframe.height(h);
	}

	jQuery(document).ready(function() {
		initTree();
		var winHeight = $(window).height() - 50;
		$('#logFrame').attr('height', winHeight);
		$("#tree").find('span').each(function() {
			if ($(this).hasClass('ico_docu')) {
				$(this).removeClass("button");
				$(this).removeClass("ico_docu");
			}
			if ($(this).hasClass('ico_open')) {
				$(this).removeClass("button");
				$(this).removeClass("ico_open");
			}

		});

	});
</script>
